package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMannerMapper;
import com.sky.mapper.addressMapper;
import com.sky.mapper.orderMapper;
import com.sky.result.PageResult;
import com.sky.service.orderMannerService;
import com.sky.utils.WeChatPayUtil;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class OrderMannerServiceImpI implements orderMannerService {
    @Autowired
    private OrderMannerMapper orderMannerMapper;
    @Autowired
    private orderMapper orderMapper;
    @Autowired
    private addressMapper addressMapper;
    @Autowired
    private WeChatPayUtil weChatPayUtil;

    @Override
    public PageResult serchOrder(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageHelper.startPage(ordersPageQueryDTO.getPage(),ordersPageQueryDTO.getPageSize());
        Page<OrderVO> serch = orderMannerMapper.serch(ordersPageQueryDTO);
        for (OrderVO orderVO : serch) {

            List<OrderDetail> listOrderDetailByOrderId = orderMapper.getListOrderDetailByOrderId(orderVO.getNumber());
            StringBuilder sb=new StringBuilder();

            for (OrderDetail orderDetail : listOrderDetailByOrderId) {
                sb.append(orderDetail.toString());
            }
            orderVO.setOrderDishes(sb.toString());
     }
        log.info("{}",serch.toString());
        return new PageResult(serch.getTotal(),serch);
    }

    @Override
    public OrderVO details(String id) {
        Orders byId = orderMapper.findById(Integer.parseInt(id));
        List<OrderDetail> listOrderDetailByOrderId = orderMapper.getListOrderDetailByOrderId(byId.getNumber());
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(byId,orderVO);
        orderVO.setOrderDetailList(listOrderDetailByOrderId);
        return orderVO;
    }

    @Override
    public void reject(OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        //根据id查出该订单其他信息
        Orders byId = orderMapper.findById(Math.toIntExact(ordersRejectionDTO.getId()));
        byId.setStatus(Orders.CANCELLED);
        byId.setCancelTime(LocalDateTime.now());
        byId.setPayStatus(Orders.REFUND);
        //退款
        weChatPayUtil.refund(byId.getNumber(),byId.getNumber(),
                new BigDecimal(0.01),
                new BigDecimal(0.01));
        //将数据库中该订单信息修改
        orderMapper.update(byId);
    }

    @Override
    public void cancelOrder(OrdersCancelDTO ordersCancelDTO) throws Exception {
        //判断用户是否付款
        Orders byId = orderMapper.findById(Math.toIntExact(ordersCancelDTO.getId()));
        byId.setStatus(Orders.CANCELLED);
        byId.setCancelTime(LocalDateTime.now());
        byId.setCancelReason(ordersCancelDTO.getCancelReason());
        if(byId.getPayStatus()==Orders.PAID){
            //退款
            weChatPayUtil.refund(byId.getNumber(),byId.getNumber(),
                    new BigDecimal(0.01),
                    new BigDecimal(0.01));
            byId.setPayStatus(Orders.REFUND);
        }
        orderMapper.update(byId);
    }
}
